package com.ruowei.modules.member.web.rest;

import com.ruowei.common.error.exception.BadRequestAlertException;
import com.ruowei.common.error.exception.CommonException;
import com.ruowei.modules.member.domain.Member;
import com.ruowei.modules.member.repository.MemberRepository;
import com.ruowei.modules.member.service.MemberService;
import com.ruowei.modules.sys.utils.AppletTokenUtils;
import com.ruowei.security.CurrentUser;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * REST controller for managing {@link Member}.
 */
@RestController
@RequestMapping("/api")
@Transactional
public class MemberResource {

    private final Logger log = LoggerFactory.getLogger(MemberResource.class);

    private final MemberService memberService;

    public MemberResource(MemberService memberService) {
        this.memberService = memberService;
    }

    @PostMapping("/getOpenIdByCode")
    @ApiOperation(value = "根据小程序code获取openId，返回自定义小程序登录态")
    public ResponseEntity<Map<String, String>> getOpenIdByCode(@RequestParam String code) {
        Map<String, String> map = new HashMap<>();
        map.put("token", memberService.getOpenIdByCode(code));
        return ResponseEntity.ok(map);
    }

    @GetMapping("/testToken")
    @ApiOperation(value = "测试小程序登录态")
    public ResponseEntity<Member> testToken(HttpServletRequest request) {
        Member result = AppletTokenUtils.getOpenIdByToken(request);
        return ResponseEntity.ok(result);
    }

}
